package com.jianbing.service.impl;

import java.util.Date;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import com.jianbing.dao.MongoDBDao;
import com.jianbing.entity.AccessLog;
import com.jianbing.entity.AccessToken;
import com.jianbing.entity.Customer;
import com.jianbing.entity.Result;
import com.jianbing.service.OrderService;
import com.jianbing.util.Constants;
import com.jianbing.util.SignUtil;
import com.jianbing.util.StringUtils;
import com.jianbing.util.TableUtil;
import com.mongodb.BasicDBObject;

import net.sf.json.JSONObject;

@Service
public class OrderServiceImpl implements OrderService {

	Logger logger = Logger.getLogger(OrderServiceImpl.class);

	@Override
	public Result getOrderSn(JSONObject json, Result result, String accessIp) throws Exception {
		ModelMap model = new ModelMap();
		Date accessDate = new Date();
		JSONObject jsonParams = json.getJSONObject("params");
		if (StringUtils.isEmpty(jsonParams.getString("appKey")) || StringUtils.isEmpty(jsonParams.getString("token"))
				|| StringUtils.isEmpty(jsonParams.getString("timestamp"))
				|| StringUtils.isEmpty(json.getString("sign"))) {
			result.setCode(101);
			result.setMsg("参数错误");
			return result;
		}
		if ((jsonParams.getLong("timestamp") + Constants.accessTime * 1000) < accessDate.getTime()) {
			result.setCode(102);
			result.setMsg("时间戳超时");
			return result;
		}
		BasicDBObject queryObject = new BasicDBObject();
		queryObject.append("appKey", jsonParams.getString("appKey")).append("token", jsonParams.getString("token"));
		AccessToken accessToken = MongoDBDao.findOne(TableUtil.accessToken, AccessToken.class, queryObject);
		if (accessToken == null) {
			result.setCode(103);
			result.setMsg("无效的token");
			return result;
		} else {
			if (accessToken.getActiveTime() < accessDate.getTime()) {
				result.setCode(104);
				result.setMsg("token已失效");
				return result;
			}
		}
		BasicDBObject queryCustomerObject = new BasicDBObject();
		queryCustomerObject.append("appKey", jsonParams.getString("appKey"));
		Customer customer = MongoDBDao.findOne(TableUtil.customer, Customer.class, queryCustomerObject);
		if (customer == null) {
			result.setCode(105);
			result.setMsg("无效的appKey");
			return result;
		}
		if (!SignUtil.checkSign(json.getString("sign"), jsonParams, customer.getAppSecret())) {
			result.setCode(106);
			result.setMsg("签名错误");
			return result;
		}

		UUID orderSn = UUID.randomUUID();
		String log = "appKey=" + jsonParams.getString("appKey") + "，获取订单号：" + orderSn;
		logger.info(log);
		model.addAttribute("orderSn", orderSn);
		result.setData(model);
		AccessLog accessLog = new AccessLog();
		accessLog.setAccessDate(accessDate);
		accessLog.setAccessIp(accessIp);
		accessLog.setAppkey(jsonParams.getString("appKey"));
		accessLog.setMsg(log);
		MongoDBDao.insert(TableUtil.accessLog, accessLog);
		return result;
	}
}
